package org.zeqp.chatAI.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.zeqp.chatAI.aiterface.audio.AuDioUtils;
import org.zeqp.chatAI.aiterface.chat.HttpChatUtils;
import org.zeqp.chatAI.aiterface.chat.SendData;
import org.zeqp.chatAI.aiterface.chat.chathistory.MyChatHistory;
import org.zeqp.chatAI.aiterface.cover.AudioCoverUtils;
import org.zeqp.chatAI.aiterface.userchat.ChatData;
import org.zeqp.chatAI.redis.RedisRepository;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class OpenChatController {


    private static final String ACCESSTOKEN="ACCESSTOKEN";

    private static final String HISTORYCHAT="history:chat:";

    @Autowired
    RedisRepository redisRepository;

    @PostMapping("/utils/openchat")
    public ResponseEntity openChat(@RequestBody Map<String,Object> params){
        String content = params.get("content").toString();
        String userName = params.get("userName").toString();
        String accessToken = redisRepository.get(ACCESSTOKEN);
        if(StringUtils.isEmpty(accessToken)){
            accessToken = HttpChatUtils.getAccessToken();
            redisRepository.set(ACCESSTOKEN,accessToken);
            redisRepository.expire(ACCESSTOKEN,60*1000*5);
        }

        MyChatHistory chatHistory = redisRepository.get(HISTORYCHAT+userName,MyChatHistory.class);
        if(chatHistory!=null){
            ChatData chatData = HttpChatUtils.sendChatData(accessToken, userName, content, chatHistory.getSendData());
            List<SendData> sendData = chatData.getUserHistoryData();
            chatHistory.setSendData(sendData);
            //覆盖
        }else {
            chatHistory = new MyChatHistory();
            chatHistory.setUserName(userName);
            List<SendData> historyData = new ArrayList<>();
            ChatData chatData = HttpChatUtils.sendChatData(accessToken, userName, content, historyData);
            if (chatHistory.getSendData() == null) {
                chatHistory.setSendData(historyData);
            }
        }
        redisRepository.set(HISTORYCHAT+userName,chatHistory);
        return new ResponseEntity<>(chatHistory,HttpStatus.OK);
    }

    @PostMapping("/audio/openchat")
    public  ResponseEntity  open(@RequestParam("file") MultipartFile file,@RequestParam("userName") String userName )  {
        InputStream inputStream = null;
        File outFile = null;
        FileInputStream fileInputStream = null;
        try {
            inputStream = file.getInputStream();
            outFile = AudioCoverUtils.convertMP32Pcm(inputStream, IdUtil.simpleUUID() + ".pcm");
            fileInputStream = new FileInputStream(outFile);
            byte[] bytes = IoUtil.readBytes(fileInputStream);
            String transFormatData = AuDioUtils.sendAudioData(bytes);
            Map<String,Object> data = new HashMap<>();
            data.put("content",transFormatData);
            data.put("userName",userName);
            return this.openChat(data);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(outFile.exists()){
                outFile.deleteOnExit();
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }




}
